var totalPage;
var totalCnt;
var mailTempData = [];
var deleteflag ;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	setTimeout(hideLoading, 500);
})
function searchMailTemp() {
	$("#page").val(1);
	$("#currentPage").val(1);
	search();
}
function search() {
	clearTable("mailTempTbody");
	$("#chkAll").attr("checked", false);
	seach=1;
	showLoading();
	$.ajax({
		type : 'post',
		data : $("#mailTempSearch").serialize(),
		url : 'searchMailTemplate',
		dataType : 'json',
		success : function(data) {
			setTimeout(hideLoading, 500);
			totalPage = data && data.totalPage || 0;
			totalCnt = data && data.totalCnt || 0;
			mailTempData = data && data.mailTemplateList || [];
			document.getElementById("total").innerText = '共' + totalCnt + '条 / 共' + totalPage + '页';
			var currentPage = $('#currentPage').val();
			var rows = $('#rows').val();
			var length = mailTempData.length;
			if (length == 0) {
				showMessge("没有查询结果");
			} else {
					for(var i=0; i<length; i++){
					var rowNo = (currentPage - 1) * rows + i + 1;
					var tbody = $("#mailTempTbody");
					var $tr = $('<tr>').appendTo(tbody);
					$("<td>").html('<input name="chk" type="checkbox">').appendTo($tr)
					$("<td>").html(rowNo).appendTo($tr);
					$("<td>").html(mailTempData[i].mailName).appendTo($tr);
					$("<td>").html(mailTempData[i].mailType).appendTo($tr);
					$("<td>").html(mailTempData[i].mailContent).appendTo($tr);
				}
			}
		},
		error : function(data) {
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge("您没有操作权限!");
			} else {
				showMessge("查询失败!");
			}
		},
		timeout: function(data){
			setTimeout(hideLoading, 500);
			showMessge("操作超时！");
		}
	})
}
function save(){
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '添加邮件模板信息';
    $("#save").modal("show");
    $("#saveMailID").attr("disabled",false);
    deleteflag = "save";
	saveTitle.append(strong);
	formReset("saveForm");
}
function update(){
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count;
	var index = checkbox && checkbox.row_index;
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '修改邮件模板信息';
		if (count == 1) {
			$('#save').modal('show');
			$("#saveMailID").val(mailTempData[index].mailID).attr("disabled",true);
			$("#saveMailName").val(mailTempData[index].mailName);
			$("#saveMailType").val(mailTempData[index].mailType);
			$("#saveMailContent").val(mailTempData[index].mailContent);
		} else if (count == 0) {
			showMessge("请选择要操作的数据!");
		} else {
			showMessge("抱歉,不能选择多行!");
		}
		deleteflag = "update";
	//初始化是否为空
	saveTitle.append(strong);
}
function confirm(){
	var saveMailID = $("#saveMailID").val();
	var saveMailName = $("#saveMailName").val();
	var saveMailType = $("#saveMailType").val();
	var saveMailContent = $("#saveMailContent").val();
	if (!saveMailID) {
		showMessge("模板ID不能为空！");
		return;
	}
	if (!saveMailName) {
		showMessge("模板名称不能为空！");
		return;
	}
	if (!saveMailType) {
		showMessge("模板类型不能为空！");
		return;
	}
	if (!saveMailContent) {
		showMessge("模板内容不能为空！");
		return;
	}
			
	if(deleteflag == "save"){
		showLoading();
		$.ajax({
			type: "POST",
			url: 'saveMailTemplate',
	    	data: {
	    		MailID : saveMailID,
	    		MailName : saveMailName,
	    		MailType : saveMailType,
				MailContent : saveMailContent,
	    	},
	    	dataType : 'json',
			success: function(data){
				setTimeout(hideLoading, 500);
				$('#save').modal('hide');
				var result = data && data.result;
				if (result == "Success") {
					showMessge("添加成功");
				} else if (result == "MailIDNoNull") {
					showMessge("模板ID已存在，请重新输入");
				} else if (result == "MailTypeNoNull") {
					showMessge("模板类型已存在，请重新输入");
				} else if (result == "NotFound") {
					showMessge("添加失败");
				} else {
					showMessge("添加异常");
				}
				search();
				formReset("saveForm");
			},
			error : function(data) {
			setTimeout(hideLoading, 500);
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge("您没有操作权限!");
				} else {
					showMessge("添加失败!");
				}
			},
			timeout: function(data){
				setTimeout(hideLoading, 500);
				showMessge("操作超时！");
			}
		});
	}else if(deleteflag == "update"){
		showLoading();
		$.ajax({
			type : "post",
			url : "updateMailTemplate",
			dataType : "json",
			data : {
				MailID : saveMailID,
	    		MailName : saveMailName,
	    		MailType : saveMailType,
				MailContent : saveMailContent,
			},
			success : function(data) {
				setTimeout(hideLoading, 500);
				$('#save').modal('hide');
				var result = data && data.result;
				if (result == "Success") {
					showMessge("修改成功");
				} else if (result == "MailTypeNoNull") {
					showMessge("模板类型已存在，请重新输入");
				} else if (result == "NotFound") {
					showMessge("修改失败");
				} else {
					showMessge("修改异常");
				}
				search();
			},
			error : function(data) {
				setTimeout(hideLoading, 500);
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge("您没有操作权限!");
				} else {
					showMessge("修改失败!");
				}
			},
			timeout: function(data){
				setTimeout(hideLoading, 500);
				showMessge("操作超时！");
			}
		});
	}
}
function sure(){
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count || 0;
	var index = undefined;
	var mailID = undefined;
	var mailIDList = [];
	for (var i=0; i<count; i++) {
		index = checkbox && checkbox.row_index[i] || 0;
		mailID = mailTempData && mailTempData[index] && mailTempData[index].mailID;
		mailIDList.push(mailID);
	}
	if(saveFlag=="delete"){
		showLoading();
		$.ajax({
			type : "post",
			url : "deleteMailTemplate",
			dataType : "json",
			data : {
				mailIDList : mailIDList.toString()
			},
			success : function(data) {
				showLoading();
				var result = data && data.result;
				if (result == "Success") {
					showMessge("删除成功");
				} else if (result == "NotFound") {
					showMessge("删除数据不存在");
				} else {
					showMessge("删除失败");
				}
				search();
			},
			error : function(data) {
				showLoading();
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge("没有访问权限!");
				} else {
					showMessge("操作失败!");
				}
			},
			timeout: function(data){
				showMessge("操作超时！");
			}
		})
		setTimeout(hideLoading, 500);
	}
}
